package com.example.industry.dao.impl;

import com.example.industry.MySqlConnector;
import com.example.industry.enums.TaskStatus;
import com.example.industry.dao.TaskDao;
import com.example.industry.entity.Task;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Repository
public class TaskDaoImpl implements TaskDao {
    // TODO
    // 修改查询任务，查询多个表填充数据放到task中返回
    @Override
    public Task findById(Integer id) {
        Task task = new Task();
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from task where task_id = " + id;
            try (Statement stmt = conn.createStatement()) {
                ResultSet resultSet = stmt.executeQuery(sql);
                if (resultSet.next()) {
                    task.setTaskId(resultSet.getInt("task_id"));
                    task.setIntroduction(resultSet.getString("introduction"));
                    if (resultSet.getString("status").equals("RUNNING")){
                        task.setStatus(TaskStatus.RUNNING);
                    }else task.setStatus(TaskStatus.DONE);
                } else {
                    task = null;
                }
            }
            connector.closeConnection();
            return task;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    // 删除任务完成
    @Override
    public Boolean deleteById(Integer id) {
        if (findById(id) == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql1 = "delete from project_task where task_id = " + id;
            String sql2 = "delete from task_accept where task_id = " + id;
            String sql3 = "delete from task_assign where task_id = " + id;
            String sql4 = "delete from task where task_id = " + id;
            try (Statement stmt = conn.createStatement()) {
                // 从project_task表中删除
                int result1 = stmt.executeUpdate(sql1);
                if (result1 <= 0) return Boolean.FALSE;
                // 从task_accept表中删除
                int result2 = stmt.executeUpdate(sql2);
                if (result2 <= 0) return Boolean.FALSE;
                // 从task_assign表中删除
                int result3 = stmt.executeUpdate(sql3);
                if (result3 <= 0) return Boolean.FALSE;
                // 从task表中删除
                int result4 = stmt.executeUpdate(sql4);
                if (result4 <= 0) return Boolean.FALSE;
            }
            connector.closeConnection();
            return Boolean.TRUE;
        } catch (Exception e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    // 指派任务完成
    @Override
    public Boolean addTask(Task task) {
        if (task == null || task.getIntroduction() == null || task.getStatus() == null || task.getAssignerID() == null || task.getAssignerID() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        Integer tempTaskId = null;
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();

            try (Statement stmt = conn.createStatement())
            {
                String sql1 = "insert into task "+"(introduction,status) " +
                    "values("+ task.getIntroduction() + "," + task.getStatus() + ")";
                if(stmt.executeUpdate(sql1) < 1)return Boolean.FALSE;
                ResultSet generatedKeys = stmt.getGeneratedKeys();
                if (generatedKeys != null) {
                    tempTaskId = generatedKeys.getInt(1);
                }
                else return Boolean.FALSE;
                String sql2 = "insert into project_task (project_id,task_id) " +
                        "values(" + task.getProjectId() + "," + tempTaskId + ")";
                if (stmt.executeUpdate(sql2) < 0) return Boolean.FALSE;

                String sql3 = "insert into task_assign (task_id,user_id) "+
                        "values(" + tempTaskId + "," + task.getAssignerID() + ")";
                if(stmt.executeUpdate(sql3) < 0) return Boolean.FALSE;

                for(Integer i : task.getAccepterId()){
                    String sql = "insert into task_accept (task_id,user_id) " +
                            "values (" + tempTaskId + "," + i + ")";
                    if(stmt.executeUpdate(sql) < 0) return Boolean.FALSE;
                }
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }
    // 修改任务完成
    @Override
    public Boolean update(Task task) {
        if (task == null || task.getIntroduction() == null || task.getStatus() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "update task set " + "introduction = " + task.getIntroduction() + ",status = " + task.getStatus() +
                    " where task_id = " + task.getTaskId();
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result <= 0) return Boolean.FALSE;
            }
            connector.closeConnection();
            return Boolean.TRUE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }
}
